clc;clear
order = 7;         % 滤波器阶数
fc = 10.23e6;      % 截止频率
fs =  6*1.023*42e6; 
[b,a] = butter(order, fc/(fs/2), 'low');      %巴特沃斯
% [b,a] = ellip(order,1,20,fc/(fs/2), 'low'); %椭圆
% [b,a]=cheby1(order,1,fc/(fs/2), 'low');     %切比雪夫一型v
% [b,a]=cheby2(order,20,fc/(fs/2), 'low');    %切比雪夫二型
% load('f.mat')
 h = fir_but;
% h1 = cheby_1dB;
% h = hh;

%% 

[H] = freqz(h, 1024*10, 48000);
figure

subplot(211);

plot(abs(H));     %abs(H）取幅频特性曲线

title('幅频特性曲线');

subplot(212);

plot(angle(H));     %angle取相频特性曲线

title('相频特性曲线');

%% 设计对应幅度滤波器所需频率响应

% % 计算频率响应
[num, den] = freqz(b, a,1024);
fre = linspace(0, 1, length(num));
fu = abs(num)';

% T = fre(33)-fre(1);
% T = fre(17)-fre(1);
T = fre(65)-fre(1);
fu_cos = 0.9+0.1*cos(2*pi.*fre/T);
% fu_sin = 0.25*sin(2*pi.*fre/T);
% 绘制频率响应曲线
figure;
% fu = [fu(7:end),fu(end)*ones(1,6)];
% fu_sin = [fu_sin,fu(85:end)];
fu_cos = [fu_cos(1:61),fu(62:end)];
% plot(fre, fu);
% hold on
plot(fre,fu_cos);
title('滤波器频率响应（幅度）');
xlabel('归一化频率');
ylabel('幅度');
% 计算频率群延迟
[gd, w] = grpdelay(b, a,40);
w = linspace(0, 1, length(w));
% 绘制频率群延迟曲线
figure
plot(w, gd);
title('滤波器频率群延迟');
xlabel('归一化频率');
ylabel('群延迟');
%% 绘制频率响应曲线（正轴）

% 计算频率响应
[H, f] = freqz(h1, 1024*10, fs); 



plot(f/1e6, 20*log10(abs(H)));
hold on

% % 计算频率响应
[H, f] = freqz(h, 1024*10, fs); 


plot(f/1e6, 20*log10(abs(H)));
title('低通滤波器的频率响应');
xlabel('频率(MHz)');
ylabel('频率响应(dB)');
xlim([0,20])
legend("初相90°","初相0°")
hold off


%% 绘制频率响应曲线（正负轴）
% % % 设置频率范围
f_start = -fs/2;    % 起始频率
f_end = fs/2;       % 结束频率

% 计算频率响应
[H, f] = freqz(b,a, 1024*10, fs, 'whole'); 

% 频率从负数到正数
f_shifted = linspace(f_start, f_end, length(H));
H_shifted = fftshift(H);

plot(f_shifted/1e6, 20*log10(abs(H_shifted)));
title('低通滤波器的频率响应');
xlabel('频率/MHz');
ylabel('频率响应/dB');
xlim([-15,15])
hold on
% % 计算频率响应

[H, f] = freqz(h, 1024*10, fs, 'whole'); 

% 频率从负数到正数
f_shifted = linspace(f_start, f_end, length(H));
H_shifted = fftshift(H);

plot(f_shifted/1e6, 20*log10(abs(H_shifted)));
title('滤波器的频率响应');
xlabel('频率(MHz)');
ylabel('频率响应');
xlim([-20,20])
legend("iir","fir")
hold off




figure
% % % 绘制滤波器群时延曲线
% [gd, f] = grpdelay(b, a, 1024*10,'whole', fs);
% % 频率从负数到正数
% f_shifted = linspace(f_start, f_end, length(gd));
% H_shifted = fftshift(gd)';
% plot(f_shifted/1e6,  H_shifted/fs*1e9);
% hold on

% % 绘制滤波器群时延曲线
[gd, f] = grpdelay(h, 1024,'whole', fs);
% 频率从负数到正数
f_shifted = linspace(f_start, f_end, length(gd));
H_shifted = fftshift(gd)';
plot(f_shifted/1e6,  H_shifted/fs*1e9);
title('群时延曲线');
xlabel('频率(MHz)');
ylabel('群时延(ns)');
xlim([-10,10])
ylim([0,300])
hold off 
% legend("iir","fir")
%% 
clc;clear;
fs = 6*1.023*42e6;
load('fupin_sin_quan.mat')
load('filt1.mat')
% h = hilbert(fupin_sin_quan);
% h1 = hilbert(2*filt1-fupin_sin_quan);
% h = conj(h1)+h+hilbert(filt1)/2;
h = fupin_sin_quan;

f_start = -fs/2;    % 起始频率
f_end = fs/2;       % 结束频率

[H, f] = freqz(h, 1024, fs, 'whole'); 

% 频率从负数到正数
f_shifted = linspace(f_start, f_end, length(H));
H_shifted = fftshift(H);

plot(f_shifted/1e6,abs(H_shifted));
title('低通滤波器的频率响应');
xlabel('频率(MHz)');
ylabel('频率响应');
xlim([-15,15])













